<?php
class DoithuongController extends Controller {
    /*Trang danh sách sản phẩm đổi thưởng*/
    public function actionIndex(){
        $url = new Url();        
        $name   = isset($_GET['name']) ?$_GET['name']   :"";
        $type   = isset($_GET['type']) ?$_GET['type']   :0;
        $page   = isset($_GET['page']) ?$_GET['page']   :1;
        $count_data = AdminGiaithuong::countRowByTypeAndName($type,$name);    
        $rows_per_page = 10;            
        if($count_data % $rows_per_page == 0){
            $totalpage = floor($count_data/$rows_per_page);
        } else{
            $totalpage = floor($count_data/$rows_per_page) + 1;                             
        }        
        $get_url    = "?name=".$name."&type=".$type;
        $begin      = ($page - 1)*$rows_per_page;
        $end        = $rows_per_page;
        $data       = AdminGiaithuong::getRowByTypeAndName($type,$name,$begin,$end);  
        $page       = Paging::showPageNavigation($page,$totalpage,$url->createUrl('doithuong/index').$get_url."&");        
        $this->render('index',array("data"=>$data,"page"=>$page,"name"=>$name,"type"=>$type,"count_data"=>$count_data,"begin"=>$begin));
    }
    
    /*Ajax delete sản phẩm*/
    public function actionDeleteRow(){
        $id = isset($_POST['id'])? intval($_POST['id'])     :0;
        if($id > 0){
            $del = AdminGiaithuong::deleteRowById($id);
            echo $del;
        }else{
            echo 0;
        }
    }
    
    /*Trang thêm sản phẩm*/
    public function actionAdd(){
        $this->render("add");
    }
    
    /*Ajax insert sản phẩm*/
    public function actionInsertRow(){
        $array_input = array();
        if(isset($_POST)){
            $array_input['name']        = isset($_POST['name'])     ? $_POST['name']            :"";
            $array_input['type']        = isset($_POST['type'])     ? intval($_POST['type'])    :0;
            $array_input['price']       = isset($_POST['price'])    ? intval($_POST['price'])   :0;
            $array_input['image_url']   = isset($_POST['image_url'])? $_POST['image_url']       :"";
            $insert = AdminGiaithuong::insertRow($array_input);
            echo $insert;
        }else{
            echo 0;
        }
    }
    
    /*Trang sửa sản phẩm*/
    public function actionEdit(){
        $id = isset($_GET['id'])? intval($_GET['id']):0;
        if($id > 0){
            $data = AdminGiaithuong::getRowById($id);
            
            $this->render("edit",array("data"=>$data));
        }else{
            $this->actionIndex();    
        }        
    }
    
    /*Ajax sửa sản phẩm*/
    public function actionUpdateRow(){
        $name        = isset($_POST['name'])     ? $_POST['name']            :"";
        $type        = isset($_POST['type'])     ? intval($_POST['type'])    :0;
        $price       = isset($_POST['price'])    ? intval($_POST['price'])   :0;
        $image_url   = isset($_POST['image_url'])? $_POST['image_url']       :"";
        $id          = isset($_POST['id'])       ? intval($_POST['id'])      : 0;
        if($id > 0){
            $update = AdminGiaithuong::updateRow($name,$type,$price,$image_url,$id);
            echo $update;     
        }else{
            echo 0;
        }             
    }
    
    /*Trang danh sách hóa đơn*/
    public function actionBill(){
        $url = new Url();
        $key_word   = isset($_GET['key_word'])  ? $_GET['key_word']             : "";
        $type_key   = isset($_GET['type_key'])  ? intval($_GET['type_key'])     : 0;
        $type       = isset($_GET['type'])      ? intval($_GET['type'])         : 0;
        $status     = isset($_GET['status'])    ? intval($_GET['status'])       : -1;
        $formDate   = isset($_GET['formDate'])  ? $_GET['formDate']             : "01/09/2011";
        $toDate     = isset($_GET['toDate'])    ? $_GET['toDate']               : date('d/m/Y',time());
        $page       = isset($_GET['page'])      ? $_GET['page']                 : 1;
        
        $formDate_db = strtotime(DateUtils::convertDatedb($formDate)); 
        $toDate_db   = strtotime(DateUtils::convertDatedb($toDate))+86300;
        $count_data  = AdminDoithuong::countAllRow($type_key,$key_word,$type,$status,$formDate_db,$toDate_db);
        
        $rows_per_page = 10;            
        if($count_data % $rows_per_page == 0){
            $totalpage = floor($count_data/$rows_per_page);
        } else{
            $totalpage = floor($count_data/$rows_per_page) + 1;                             
        }
        $get_url    = "?key_word=$key_word&type_key=$type_key&type=$type&status=$status&formDate=$formDate&toDate=$toDate";
        $begin      = ($page - 1)*$rows_per_page;
        $end        = $rows_per_page;
        
        $data       = AdminDoithuong::getAllRow($type_key,$key_word,$type,$status,$formDate_db,$toDate_db,$begin,$end);  
        $page       = Paging::showPageNavigation($page,$totalpage,$url->createUrl('doithuong/bill').$get_url."&");
        foreach($data as $key=>$value){
            $item[$key] = AdminDoithuong::getItemById($value['id']);    
        } 
        $this->render("bill",array("count_data"=>$count_data,"data"=>$data,"key_word"=>$key_word,"type_key"=>$type_key,"type"=>$type,"formDate"=>$formDate,"toDate"=>$toDate,"page"=>$page,"begin"=>$begin,"item"=>$item));
    }  
    
    /*Ajax update status hóa đơn đổi thưởng*/
    public function actionUpdateStatus(){
        $id     = isset($_POST['id'])       ?intval($_POST['id'])       :0;
        $status = isset($_POST['status'])   ? intval($_POST['status'])  :0;
        if($id > 0){
            $update = AdminDoithuong::updateStatusById($id,$status);
            echo $update;
        }else{
            echo 0;
        }
    }
}